package org.agile4j.plugin.gun.xml;

import java.io.File;
import java.util.Map;

import org.agile4j.plugin.gun.helper.GunHelper;
import org.agile4j.plugin.gun.utils.ParserUtil;
import org.agile4j.plugin.gun.utils.ReplaceContainerUtil;
import org.agile4j.plugin.gun.xml.dict.ComplexType;
import org.agile4j.plugin.gun.xml.dict.Element;
import org.agile4j.plugin.gun.xml.dict.Schema;
import org.agle4j.framework.annotation.Service;
import org.agle4j.framework.utils.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class GunDict {
	private static final Logger LOGGER = LoggerFactory.getLogger(GunDict.class);
	private static final String SPLIT = "." ;
	
	public boolean createTarFile() {
		return createTarFile(GunHelper.getCreateTarXmlFileUrl()) ;
	}
	
	public boolean createTarFile(String createTarFileUrl) {
		Map<String, Schema> schemaMap = ParserUtil.deplogyDict(new File(createTarFileUrl));
		if (CollectionUtil.isNotEmpty(schemaMap)) {
			for (Map.Entry<String, Schema> schemaEntry : schemaMap.entrySet()) {
				Schema schema = schemaEntry.getValue();
				if (CollectionUtil.isNotEmpty(schema.getDescriptionOrComplexType())) {
					for (Object obj : schema.getDescriptionOrComplexType()) {
						if (obj instanceof ComplexType) {
							ComplexType complexType = (ComplexType) obj;
							for (Object obj1 : complexType.getElementOrDescription()) {
								if (obj1 instanceof Element) {
									Element element = (Element) obj1;
									String elementId = element.getId();

									String newElementId = "";
									if (ReplaceContainerUtil.containsKey(element.getId())) { // 替换 id
										newElementId = ReplaceContainerUtil.getReplaceValue(element.getId());
										element.setId(newElementId);
									}

									////// Map中添加基于ref的替换集合 start
									if (StringUtils.isNotEmpty(newElementId)) {
										String key = schema.getId() + SPLIT + complexType.getId() + SPLIT + elementId;
										String value = schema.getId() + SPLIT + complexType.getId() + SPLIT + newElementId;
										ReplaceContainerUtil.putRepalceValue(key, value);
									}
									////// Map中添加基于type的替换集合 end

									if (StringUtils.isNotEmpty(element.getRef())) { // 替换 ref
										String ref = element.getRef();
										ref = StringUtils.substring(ref, StringUtils.lastIndexOf(ref, ".") + 1, StringUtils.length(ref));
										if (ReplaceContainerUtil.containsKey(ref)) {
											String replace = ReplaceContainerUtil.getReplaceValue(ref);
											StringUtils.replace(element.getRef(), ref, replace);
											element.setRef(StringUtils.replace(element.getRef(), ref, replace));
										}
									}
									
									if (ReplaceContainerUtil.containsKey(element.getType())) {	//	赋值BASE
										element.setType(ReplaceContainerUtil.getReplaceValue(element.getType()));
									}
								}
							}
						}
					}
				}

				File file = new File(schemaEntry.getKey());
				String tarFilePath = file.getParent() + File.separator + GunHelper.getTarFileFormat() + file.getName(); // 目标文件路径
				ParserUtil.replaceXmlField(tarFilePath, schema);
			}
		}
		return true;
	}
	
	public static void main(String[] args) {
		String ref = "CmDict.Comm.dianzggh" ;
		ref = StringUtils.substring(ref, StringUtils.lastIndexOf(ref, ".") + 1, StringUtils.length(ref)) ;
		LOGGER.debug(ref);
	}
}
